﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-                                           *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2018年8月8日 17:32:35
 *  文件描述:时间相关的一个工具类
 *****************************************************************************/

using System;
using UnityEngine;

namespace Util
{
    public static class TimeTool
    {
        /// <summary>
        /// 时间戳转换为时间
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static string FormatTime(int seconds)
        {
            if (seconds >= 3600)
            {
                int h = (int)Mathf.Floor(seconds / 3600);
                int m = (int)Mathf.Floor(seconds / 60) % 60;
                int s = (int)seconds % 60;
                return System.String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
            }
            else if (seconds >= 60)
            {
                int m = (int)Mathf.Floor(seconds / 60);
                int s = (int)seconds % 60;
                return System.String.Format("{0:D2}:{1:D2}", m, s);
            }
            else
            {
                return System.String.Format("00:{0:D2}", seconds);
            }
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static int GetNow()
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(DateTime.Now - startTime).TotalSeconds;
        }

        public static int GetNow(DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        public static int GetNows(this DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        public static DateTime CutTime()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// 时间戳转换为时间
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime GetDate(int timestamp)
        {
            // DateTime converted = new DateTime(1970, 1, 1);
            // DateTime newDateTime = converted.AddSeconds(timestamp);
            // return newDateTime.ToLocalTime();

            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return dtStart.AddSeconds(timestamp);
        }

        /// <summary>
        /// 比对是否是同一天
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool IsSameDay(DateTime d1, DateTime d2)
        {
            return (d1.Year == d2.Year && d1.Month == d2.Month && d1.Day == d2.Day);
        }

        /// <summary>
        /// 是否是未来的/新的一天
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool isNewDay(DateTime d1, DateTime d2)
        {
            if (d1.Year > d2.Year)
                return true;

            if (d1.Month > d2.Month)
                return true;

            if (d1.Day > d2.Day)
                return true;

            return false;
        }

        /// <summary>
        /// 获取差距的天
        /// </summary>
        public static int GetDayDelta(DateTime d1, DateTime d2)
        {
            var t = d1.Subtract(d2);
            return (int)t.TotalDays;
        }

        /// <summary>
        /// 获取差距的小时
        /// </summary>
        public static int GetDayHours(DateTime d1, DateTime d2)
        {
            var t = d1.Subtract(d2);
            return (int)t.TotalHours;
        }

        /// <summary>
        /// 获取差距的秒
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static int GetSecondsDelta(this DateTime d1, DateTime d2)
        {
            var t = d1.Subtract(d2);
            return (int)t.TotalSeconds;
        }

        /// <summary>
        /// 获取与当前时间差距的秒
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static int GetCurTtimeSecondsDelta(this DateTime d1)
        {
            var t = CutTime();
            var tz = t.Subtract(d1);
            return (int)tz.TotalSeconds;
        }

        /// <summary>
        /// 获取差距的天
        /// </summary>
        public static int GetDayDelta(int t1, int t2)
        {
            DateTime d1 = GetDate(t1);
            DateTime d2 = GetDate(t2);
            var t = d1.Subtract(d2);
            return (int)t.TotalDays;
        }

        /// <summary>
        /// 比对时间大小
        /// </summary>
        /// <param name="time"></param>
        /// <param name="t1"></param>
        /// <returns></returns>
        public static bool Compare(this DateTime time, DateTime t1)
        {
            return DateTime.Compare(time, t1) > 0;
        }

        /// <summary>
        /// 将配置的时间值转为DateTime
        /// </summary>
        /// <param name="_time">格式如1700、1159等</param>
        /// <returns></returns>
        public static DateTime IntToDateTime(int _time, bool _min)
        {
            var hour = _time / 100;
            var minute = _time % 100;
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, _min ? 0 : 59);
            //debug.log(time);
            return time;
        }

        //---------------------------------------------------------------------

        /// <summary>
        /// 获取网络时间
        /// </summary>
        /// <param name="CallBack"></param>
        public static void NetTime(Action<NetTimeBag> CallBack)
        {
            TimeNet.ins.NetTime(CallBack);
        }
    }
}